$(document).ready(function () {

   // console.log('loaded');
    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        } else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name, "", -1);
    }

    function _uGC(l, n, s) {
        if (!l || l == "" || !n || n == "" || !s || s == "") return "";
        var i, i2, i3, c = "";
        i = l.indexOf(n);
        i3 = n.indexOf("=") + 1;
        if (i > -1) {
            i2 = l.indexOf(s, i);
            if (i2 < 0) {
                i2 = l.length;
            }
            c = l.substring((i + i3), i2);
        }
        return c;
    }

    var source = "", medium = "", term = "", content = "", campaign = "", gclid = "", cookieString = "";
    var z = _uGC(document.cookie, '__utmz=', ';');
    //console.log(readCookie(__utmz));
   // console.log(z);
    if (z == "") {
        var gz = _uGC(document.cookie, 'google=', ';');
        source = _uGC(gz, 'utm_source=', '&');
        medium = _uGC(gz, 'utm_medium=', '&');
        campaign = _uGC(gz, 'utm_campaign=', '&');
        term = _uGC(gz, 'utm_term=', '&');
        content = _uGC(gz, 'utm_content=', '&');
    } else {
        source = _uGC(z, 'utmcsr=', '|');
        medium = _uGC(z, 'utmcmd=', '|');
        campaign = _uGC(z, 'utmccn=', '|');
        term = _uGC(z, 'utmctr=', '|');
        content = _uGC(z, 'utmcct=', '|');
        gclid = _uGC(z, 'utmgclid=', '|');
    }


    cookieString = 'source=' + source + '&medium=' + medium + '&campaign=' + campaign + '&term=' + term + '&content=' + content;
    createCookie('gaCookie', cookieString);


});


